<template>
  <view class="page_wrap_container">
    <!-- #ifndef H5 -->
    <view class="welcome_text_app">欢迎登录SOP</view>
    <view class="fixed_login_btn" @click="handleLogin">
      <text>钉钉授权登录</text>
      <view class="iconButton">
        <u-icon name="arrow-rightward" color="#fff" size="24"></u-icon>
      </view>
    </view>
    <!-- #endif -->
    <!-- #ifdef H5 -->
    <view class="gys_login">
      <view class="form">
        <view class="welcome_text">欢迎登录SOP</view>
        <view class="flex-column">
          <label>组织编码： </label>
        </view>
        <view class="inputForm">
          <image src="../../static/images/index/org_login.png" />
          <input type="text" class="input" placeholder="请输入组织编码" v-model="gysModal.orgCode">
        </view>
        <view class="flex-column">
          <label>手机号： </label>
        </view>
        <view class="inputForm">
          <image src="../../static/images/index/phone_login.png" />
          <input type="text" class="input" placeholder="请输入手机号" v-model="gysModal.phoneNumber">
        </view>
        <view class="flex-column">
          <label>密码： </label>
        </view>
        <view class="inputForm">
          <image src="../../static/images/index/pass_login.png" />
          <input type="password" class="input" placeholder="请输入密码" v-model="gysModal.password">
        </view>
        <button class="button-submit" @click="GYSLogin">登录</button>
      </view>
    </view>
    <!-- #endif -->
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import HTTP from '@/utils/http'
import { reactive } from 'vue';
async function handleLogin() {
  uni.showLoading({ title: '正在登录中' })
  const loginRes = await uni.login().catch((err) => console.log(err, '登录失败了'))
  HTTP.POST({
    url: 'auth/auth/loginByAppAuthCode',
    data: { authCode: loginRes!.code },
    isJson: true,
    success: (res: any) => {
      uni.setStorageSync('token', res?.data?.accessToken || '')
      uni.setStorageSync('user_info', res?.data?.userDTO || {})
      uni.reLaunch({ url: '/pages/index/index' })
      uni.showToast({
        title: '欢迎登录',
        icon: 'none',
      })
    },
    complete: () => {
      uni.hideLoading()
    },
  })
}
const gysModal = reactive({
  orgCode: "",
  phoneNumber: "",
  password: ""
})
const GYSLogin = () => {
  if (!gysModal.orgCode) {
    uni.showToast({
      icon: "none",
      title: "请输入组织编码"
    })
    return
  }
  if (!gysModal.phoneNumber) {
    uni.showToast({
      icon: "none",
      title: "请输入手机号"
    })
    return
  }
  if (!gysModal.password) {
    uni.showToast({
      icon: "none",
      title: "请输入密码"
    })
    return
  }
  uni.showLoading({ title: '正在登录中' })
  const params = {
    applicationId: "3",
    orgType: 1,
    ...gysModal,
  }
  HTTP.POST({
    url: "auth/auth/login",
    isJson: true,
    data: params,
    success: (res: any) => {
      if (res.code == 200) {
        uni.setStorageSync('token', res?.data?.accessToken || '')
        uni.setStorageSync('user_info', res?.data?.userDTO || {})
        uni.reLaunch({ url: '/pages/index/index' })
        uni.showToast({
          title: '欢迎登录',
          icon: 'none',
        })
      }
    },
    complete: () => {
      uni.hideLoading()
    }
  })

}
</script>

<style lang="scss" scoped>
.page_wrap_container {
  width: 100vw;
  height: 100vh;
  background: url('../../static/images/index/login_bgc.webp') no-repeat;
  background-size: 100% 100%;
  background-position: center center;
  position: relative;

  .arrow_fixed {
    position: fixed;
    width: 26px;
    height: 26px;
    left: 30px;
  }

  .welcome_text_app {
    position: fixed;
    left: 30px;
    top: 15%;
    font-size: 50rpx;
    color: #fff;
    letter-spacing: 0.06em;
  }

  .fixed_login_btn {
    position: fixed;
    top: 70%;
    left: 50%;
    transform: translate(-50%, 0);
    background: $uni-color-primary;
    color: white;
    font-family: inherit;
    padding: 0.45em;
    padding-left: 1em;
    font-size: 17px;
    font-weight: 500;
    border-radius: 0.9em;
    border: none;
    cursor: pointer;
    letter-spacing: 0.05em;
    display: flex;
    align-items: center;
    box-shadow: inset 0 0 1.6em -0.6em $uni-color-primary;
    overflow: hidden;
    height: 2.8em;
    padding-right: 3em;

    .iconButton {
      margin-left: 1em;
      position: absolute;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 2.2em;
      width: 2.2em;
      border-radius: 0.7em;
      box-shadow: 0.1em 0.1em 0.6em 0.2em #4eacb4;
      right: 0.3em;
      transition: all 0.3s;
    }

    .contactButton:hover {
      transform: translate(-0.05em, -0.05em);
      box-shadow: 0.15em 0.15em #a2edf2;
    }

    .contactButton:active {
      transform: translate(0.05em, 0.05em);
      box-shadow: 0.05em 0.05em #a2edf2;
    }
  }
}
</style>

<style lang="scss" scoped>
.gys_login {
  position: fixed;
  top: 16%;
  left: 50%;
  transform: translate(-50%, 0);
  width: 100%;

  .welcome_text {
    color: #000;
    font-size: 50rpx;
    text-align: center;
    padding: 24rpx 0;
  }

  .form {
    margin: 0 30rpx;
    display: flex;
    flex-direction: column;
    gap: 10px;
    border: 1px solid #fff;
    background-color: #fff;
    padding: 30px;
    border-radius: 20px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  }

  ::placeholder {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  }

  .form button {
    align-self: flex-end;
  }

  .flex-column>label {
    color: #333;
    font-weight: 600;
  }

  .inputForm {
    border: 1.5px solid #ecedec;
    border-radius: 10px;
    height: 50px;
    display: flex;
    align-items: center;
    padding-left: 10px;
    transition: 0.2s ease-in-out;

    image {
      width: 20px;
      height: 20px;
    }
  }

  .input {
    margin-left: 10px;
    border-radius: 10px;
    border: none;
    width: 85%;
    height: 100%;
  }

  .input:focus {
    outline: none;
  }

  .inputForm:focus-within {
    border: 1.5px solid #42bac3;
  }

  .flex-row {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    justify-content: space-between;
  }

  .flex-row>div>label {
    font-size: 14px;
    color: black;
    font-weight: 400;
  }

  .span {
    font-size: 14px;
    margin-left: 5px;
    color: #42bac3;
    font-weight: 500;
    cursor: pointer;
  }

  .button-submit {
    margin: 20px 0 10px 0;
    background-color: #42bac3;
    border: none;
    color: white;
    font-size: 15px;
    font-weight: 500;
    border-radius: 10px;
    height: 40px;
    line-height: 40px;
    width: 100%;
    cursor: pointer;
  }

  .button-submit:hover {
    background-color: #42bac3;
  }

  .p {
    text-align: center;
    color: black;
    font-size: 14px;
    margin: 5px 0;
  }
}
</style>
<style>
@media screen and (min-width: 1200px) {
  .form {
    max-width: 40% !important;
    margin: 0 auto !important;
  }
}

@media screen and (min-width: 768px) and (max-width: 1200px) {
  .form {
    max-width: 50% !important;
    margin: 0 auto !important;
  }
}
</style>